package org.qs.webservice.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;

import org.qs.webservice.exception.QSReadException;
import org.qs.webservice.service.UserService;
import org.qs.webservice.util.IReadUtils;
import org.qs.webservice.util.StringUtils;
import org.springframework.stereotype.Component;


@Component
@Path("/v{version}/auth")
public class UserRestController {
	@Resource
	private UserService userService;
	/**
	 * 注册
	 * @param mobile_num 手机号 可以为空
	 * @param user_name 用户名
	 * @param password 密码
	 * @param device_identifier 设备号
	 * @param promot 渠道
	 * @return
	 */
	@POST 
    @Path("/register")
	public String register(@FormParam("mobile_num")String mobile_num,
			@FormParam("user_name")String user_name,
			@FormParam("password")String password,
			@FormParam("email")String email,
			@FormParam("device_identifier")String device_identifier,
			@FormParam("promot")String promot,
			@FormParam("sign")String sign,
			@Context HttpServletRequest request){
		try{
			mobile_num=IReadUtils.preventInjection(mobile_num);
			//password=IReadUtils.preventInjection(password);
			device_identifier=IReadUtils.preventInjection(device_identifier);
			promot=IReadUtils.preventInjection(promot);
			String regist_ip = request.getRemoteAddr();
			String str = userService.registerUser(user_name,mobile_num,  password, device_identifier, promot,email,regist_ip,sign);
			return str;
		}catch(Exception e){
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("mobile_num", mobile_num);
			errorJson.put("password", password);
			errorJson.put("device_identifier", device_identifier);
			errorJson.put("promot", promot);
			QSReadException.printf(e, "注册异常",errorJson);
			Map<String,Object> tokenMap = new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	/**
	 * 注册完登录
	 * @Title: customLogin 
	 * @param phone
	 * @param key
	 * @param device_identifier
	 * @param promot
	 * @param channel
	 * @return   
	 * @return String    
	 * @throws
	 */
	@POST
    @Path("/login/custom")
	public String customLogin(@FormParam("user_name")String user_name,
			@FormParam("password")String password,
			@FormParam("sign")String sign,
			@HeaderParam("Info-Uuid")String device_identifier){
		try{
			user_name=IReadUtils.preventInjection(user_name);
			//password=IReadUtils.preventInjection(password);
			device_identifier=IReadUtils.preventInjection(device_identifier);
			String str = userService.customLogin(user_name, password,device_identifier,sign);
			return str;
		}catch(Exception e){
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("user_name", user_name);
			errorJson.put("password", password);
			errorJson.put("device_identifier", device_identifier);
			QSReadException.printf(e, "登录异常",errorJson);
			Map<String,Object> tokenMap = new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","参数错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	
	/**
	 * 单点登录
	 * @Title: ssoLogin 
	 * @param user_identifier 第三方获取的唯一标识�?qq=>openid wechat=>unionid baidu=>userid
	 * @param device_identifier 终端唯一标识
	 * @param nick_name 用户昵称
	 * @param source 登录注册渠道：手机快速:1, 微信:2, qq:3, 百度:4 自主注册:5
	 * @param promot 内置渠道
	 * @return   
	 * @return String    
	 * @throws
	 */
	@POST 
    @Path("/login/sso")
	public String ssoLogin(@FormParam("user_identifier")String user_identifier,
			@FormParam("device_identifier")String device_identifier,
			@FormParam("nick_name")String nick_name,
			@FormParam("source")String source,
			@FormParam("promot")String promot,
			@FormParam("img_url")String img_url,
			@HeaderParam("token")String token,
			@Context HttpServletRequest request){
		try{
			user_identifier=IReadUtils.preventInjection(user_identifier);
			device_identifier=IReadUtils.preventInjection(device_identifier);
			nick_name=IReadUtils.preventInjection(nick_name);
			source=IReadUtils.preventInjection(source);
			promot=IReadUtils.preventInjection(promot);
			String regist_ip = request.getRemoteAddr();
			String str = userService.ssoLogin(user_identifier, device_identifier, nick_name, source, promot,token,img_url,regist_ip);
			return str;
		}catch(Exception e){
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("user_identifier", user_identifier);
			errorJson.put("device_identifier", device_identifier);
			errorJson.put("nick_name", nick_name);
			errorJson.put("source", source);
			errorJson.put("promot", promot);
			QSReadException.printf(e, "第三方登录！",errorJson);
			Map<String,Object> tokenMap = new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","参数错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	@POST 
    @Path("/login/third")
	public String addThirdLogin(@FormParam("user_identifier")String user_identifier,
			@FormParam("device_identifier")String device_identifier,
			@FormParam("nick_name")String nick_name,
			@FormParam("source")String source,
			@FormParam("promot")String promot,
			@FormParam("img_url")String img_url,
			@HeaderParam("token")String token,
			@Context HttpServletRequest request){
		try{
			user_identifier=IReadUtils.preventInjection(user_identifier);
			device_identifier=IReadUtils.preventInjection(device_identifier);
			nick_name=IReadUtils.preventInjection(nick_name);
			source=IReadUtils.preventInjection(source);
			promot=IReadUtils.preventInjection(promot);
			String regist_ip = request.getRemoteAddr();
			String str = userService.addThirdLogin(user_identifier, device_identifier, nick_name, source, promot,token,img_url,regist_ip);
			return str;
		}catch(Exception e){
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("user_identifier", user_identifier);
			errorJson.put("device_identifier", device_identifier);
			errorJson.put("nick_name", nick_name);
			errorJson.put("source", source);
			errorJson.put("promot", promot);
			QSReadException.printf(e, "第三方登录！",errorJson);
			Map<String,Object> tokenMap = new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","参数错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
/*	@POST 
    @Path("/login/bind")
	public String addBindLogin(@FormParam("user_identifier")String user_identifier,
			@FormParam("device_identifier")String device_identifier,
			@FormParam("nick_name")String nick_name,
			@FormParam("source")String source,
			@FormParam("promot")String promot,
			@FormParam("img_url")String img_url,
			@FormParam("sign")String sign,
			@HeaderParam("token")String token,
			@Context HttpServletRequest request){
		try{
			user_identifier=IReadUtils.preventInjection(user_identifier);
			device_identifier=IReadUtils.preventInjection(device_identifier);
			nick_name=IReadUtils.preventInjection(nick_name);
			source=IReadUtils.preventInjection(source);
			promot=IReadUtils.preventInjection(promot);
			String regist_ip = request.getRemoteAddr();
			String str = userService.addThirdLogin(user_identifier, device_identifier, nick_name, source, promot, token, img_url, regist_ip);
			return str;
		}catch(Exception e){
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("user_identifier", user_identifier);
			errorJson.put("device_identifier", device_identifier);
			errorJson.put("nick_name", nick_name);
			errorJson.put("source", source);
			errorJson.put("promot", promot);
			QSReadException.printf(e, "第三方登录！",errorJson);
			Map<String,Object> tokenMap = new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","参数错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}*/
	/**
	 * 发送重置密码邮件
	 * @param email
	 * @return
	 */
	@POST 
	@Path("/reset/email")
	public String sendEmail(@FormParam("email")String email,@FormParam("type")String type
			){
		try{
			int type_int = 0;
			try {
				if(!StringUtils.isNullOrEmpty(type))
					type_int = Integer.valueOf(type);
			} catch (Exception e) {
			}
			//password=IReadUtils.preventInjection(password);
			email=IReadUtils.preventInjection(email);
			String str = userService.sendEmail(email,type_int);
			return str;
		}catch(Exception e){
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("email", email);
			QSReadException.printf(e, "发送邮件异常",errorJson);
			Map<String,Object> tokenMap = new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","发送邮件异常");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	/**
	 * 验证地址是否有效
	 * @param sign
	 * @return
	 */
	@POST 
	@Path("/check/sign")
	public String checkSign(@FormParam("sign")String sign
			){
		try{
			//password=IReadUtils.preventInjection(password);
			
			String str = userService.checkSign(sign,0);
			return str;
		}catch(Exception e){
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("sign", sign);
			QSReadException.printf(e, "验证sign异常",errorJson);
			Map<String,Object> tokenMap = new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","验证地址异常");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	/**
	 * 重置密码
	 * @param sign
	 * @param password
	 * @return
	 */
	@POST 
	@Path("/reset/passwd")
	public String resetPasswd(@FormParam("sign")String sign,@FormParam("password")String password
			){
		try{
			
			
			String str = userService.resetPasswd(sign,password);
			return str;
		}catch(Exception e){
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("sign", sign);
			errorJson.put("password", password);
			QSReadException.printf(e, "重置密码异常",errorJson);
			Map<String,Object> tokenMap = new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	@GET 
	@Path("/test")
	public String test(@QueryParam("sign")String sign,@QueryParam("password")String password
			){
		try{
			
			
			String str = userService.resetPasswd(sign,password);
			return str;
		}catch(Exception e){
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("sign", sign);
			errorJson.put("password", password);
			QSReadException.printf(e, "重置密码异常",errorJson);
			Map<String,Object> tokenMap = new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	/**
	 * 验证邮箱是否可用
	 * @param email
	 * @return
	 */
	@GET
    @Path("/check/email")
	public String checkEmail(@QueryParam("email")String email){
		try{
			email=IReadUtils.preventInjection(email);
			String str = userService.checkEmail(email);
			return str;
		}catch(Exception e){
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("email", email);
			QSReadException.printf(e, "验证邮箱异常",errorJson);
			Map<String,Object> tokenMap = new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	/**
	 * 验证用户名可用
	 * @param user_name
	 * @return
	 */
	@GET
    @Path("/check/user")
	public String checkUser(@QueryParam("user_name")String user_name){
		try{
			user_name=IReadUtils.preventInjection(user_name);
			String str = userService.checkUser(user_name);
			return str;
		}catch(Exception e){
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("user_name", user_name);
			QSReadException.printf(e, "验证用户名异常",errorJson);
			Map<String,Object> tokenMap = new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	@POST
    @Path("/reset/user")
	public String resetInfo(@FormParam("user_name")String user_name,
			@FormParam("mobile_num")String mobile_num,
			@FormParam("email")String email,
			@FormParam("new_password")String new_password,
			@FormParam("old_password")String old_password,
			@HeaderParam("Info-Uuid")String device_identifier,
			@HeaderParam("token")String token
			){
		try{
			user_name=IReadUtils.preventInjection(user_name);
			mobile_num=IReadUtils.preventInjection(mobile_num);
			email=IReadUtils.preventInjection(email);
			String str = userService.resetUserInfo(user_name,mobile_num,email,token,device_identifier,new_password,old_password);
			return str;
		}catch(Exception e){
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("user_name", user_name);
			QSReadException.printf(e, "修改信息异常",errorJson);
			Map<String,Object> tokenMap = new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}

}
